package com.oron3.locker;

import com.oron3.lock.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class SecretEdit extends RelativeLayout {

	private Button addButton;
	private Runnable onConfirm;
	private EditText editTitle;
	private EditText editSecret;
	private Button editButton;
	private EditText addTitle;
	private EditText addSecret;
	private State state;
	private InputMethodManager imm;

	public enum State {
		INVISIBLE, EDIT, ADD
	}

	public SecretEdit(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public SecretEdit(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SecretEdit(Context context) {
		super(context);
		init();
	}

	private void init() {
		imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.layout_widget_secret_edit, this, true);
		editTitle = (EditText) findViewById(R.id.editTitle);
		editSecret = (EditText) findViewById(R.id.editSecret);
		editButton = (Button) findViewById(R.id.editSecretButton);
		addTitle = (EditText) findViewById(R.id.addTitle);
		addSecret = (EditText) findViewById(R.id.addSecret);
		addButton = (Button) findViewById(R.id.addSecretButton);
		setState(State.INVISIBLE);
		editButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onConfirm.run();
				setEditVisibilty(View.GONE);
			}
		});
		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				onConfirm.run();
				setAddVisibilty(View.GONE);

			}
		});
	}

	protected void setAddVisibilty(int visibilty) {
		addTitle.setVisibility(visibilty);
		addSecret.setVisibility(visibilty);
		addButton.setVisibility(visibilty);
		addTitle.clearFocus();
		addSecret.clearFocus();

		imm.hideSoftInputFromWindow(addSecret.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(addTitle.getWindowToken(), 0);
	}

	protected void setEditVisibilty(int visibilty) {
		editTitle.setVisibility(visibilty);
		editSecret.setVisibility(visibilty);
		editButton.setVisibility(visibilty);
		editTitle.clearFocus();
		editSecret.clearFocus();
		imm.hideSoftInputFromWindow(editSecret.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(editTitle.getWindowToken(), 0);
	}

	public void setOnConfirm(Runnable onConfirm) {
		this.onConfirm = onConfirm;
	}

	public String getSecret() {
		return state == State.EDIT ? editSecret.getText().toString() : addSecret.getText().toString();
	}

	public String getTitle() {
		return state == State.EDIT ? editTitle.getText().toString() : addTitle.getText().toString();
	}

	public void setState(State s) {
		state = s;
		switch (s) {
		case ADD:
			setEditVisibilty(View.GONE);
			setAddVisibilty(View.VISIBLE);
			break;
		case EDIT:
			setEditVisibilty(View.VISIBLE);
			setAddVisibilty(View.GONE);
			break;
		case INVISIBLE:

			setEditVisibilty(View.GONE);
			setAddVisibilty(View.GONE);
			break;
		default:
			break;

		}
	}

	public void setSecret(Secret editingSecret) {
		editTitle.setText(editingSecret.secretTitle);
		editSecret.setText(editingSecret.secret);
	}

	public State getState() {

		return state;
	}
}
